import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'
import RecycleBin from '@/views/RecycleBin.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/home',
    name: 'Home',
    component: () => import('../views/Home.vue'),
    children: [
      {
        path: 'dashboard',
        component: () => import('../views/Dashboard.vue')
      },
      {
        path: 'users',
        name: 'UserManagement',
        component: () => import('../views/UserManagement.vue')
      },
      {
        path: 'logs',
        name: 'LogManagement',
        component: () => import('../views/LogManagement.vue')
      },
      {
        path: 'blacklist',
        name: 'IpBlacklist',
        component: () => import('../views/IpBlacklist.vue')
      },
      {
        path: 'recycle-bin',
        component: RecycleBin
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

// Simple route guard
router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    next()
  } else {
    const username = localStorage.getItem('username')
    const userRole = localStorage.getItem('userRole')
    console.log('路由守卫检查:', { username, userRole }) // 添加调试日志

    if (!username) {
      next('/login')
    } else {
      next()
    }
  }
})

export default router 